/*
  The MIT License
  
  Copyright (c) 2009 smath project
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
package pl.smath.expression.model;

/**
 * Relation term like x=3, a>4 etc
 * 
 * @author Krzysztof Langner
 *
 */
public class RelationTerm extends Term {
	
	public enum RelationType{
		EQUALITY,
		INEQUALITY,
		GREATER_THEN,
		GREATER_THEN_OR_EQUAL_TO,
		LESS_THEN,
		LESS_THEN_OR_EQUAL_TO
	};
	
	/** Lewe wyrazenie */
	private final Term leftTerm;
	/** Prawe wyrazenie */
	private final Term rightTerm;
	/** typ */
	private RelationType relationType;
	

	/**
	 * constructor
	 * @param type
	 * @param left
	 * @param right
	 */
	public RelationTerm(RelationType type, Term left, Term right) {

		super(type2string(type));
		relationType = type;
		this.leftTerm = left;
		this.rightTerm = right;
	}

	/**
	 * constructor
	 * @param type
	 * @param left
	 * @param right
	 */
	public RelationTerm(String type, Term left, Term right) {

		super(type);
		relationType = string2type(type);
		this.leftTerm = left;
		this.rightTerm = right;
	}

	/**
	 * Zmienia kierunek relacji. Np z '>' na '<'
	 */
	public void changeDirection(){
		
		if(relationType == RelationType.GREATER_THEN)
			relationType = RelationType.LESS_THEN;
		else if(relationType == RelationType.GREATER_THEN_OR_EQUAL_TO)
			relationType = RelationType.LESS_THEN_OR_EQUAL_TO;
		else if(relationType == RelationType.LESS_THEN)
			relationType = RelationType.GREATER_THEN;
		else if(relationType == RelationType.LESS_THEN_OR_EQUAL_TO)
			relationType = RelationType.GREATER_THEN_OR_EQUAL_TO;
	}
	
	/**
	 * typ relacji
	 */
	public RelationType getRelationType(){
		return relationType;
	}
	
	/**
	 * Pobranie lewej strony
	 */
	public Term getLeftTerm() {
		return leftTerm;
	}
	
	/**
	 * Pobranie prawej strony
	 */
	public Term getRightTerm() {
		return rightTerm;
	}
	
	/**
	 * Sprawdzenie czy 2 wyrazenia sa sobie równowazne.
	 * Czyli czy mozna postawic miedzy nimi znak = (w sensie matematycznym)
	 * Np. ponizsze wyrazenia sa rownowazne
	 * c*(a+b) i (b+a)*c
	 */
	public boolean isAbsTheSame(Term term){
		assert(term != null);
		if(term instanceof RelationTerm){
			return	getText().equals(term.getText()) && 
					((RelationTerm)term).getLeftTerm().equalsTo(leftTerm) &&
					((RelationTerm)term).getRightTerm().equalsTo(rightTerm);
		}
		
		return false;
	}

	/**
	 * Zamien na string
	 */
	public String toString(){
		String output;
		
		output = leftTerm.toString();

		output += type2string(relationType);

		output += rightTerm.toString();

		return output;

	}
	
	/**
	 * @return tekst wyrażenia
	 */
	public Term copy(){
		Term term = new RelationTerm(relationType, getLeftTerm().copy(), getRightTerm().copy());
		
		return term;
	}


	@Override
	public Term transform(ITransform t) {
		Term outputTerm = null;
	    Term lTerm = null;
	    Term rTerm = null;
	
	    outputTerm = t.process(this);
	    
	    if(outputTerm == this){
	    	lTerm = getLeftTerm().transform(t);
	    	rTerm = getRightTerm().transform(t);
	            
	        // Jezeli zmienily sie dzieci to tworzony jest nowy wezel
	    	if(lTerm != getLeftTerm() || rTerm != getRightTerm()){
	    		outputTerm = new RelationTerm(relationType, lTerm, rTerm);
	    		outputTerm.setNegative(isNegative());
	    	}
	    }
	    else{
	    	outputTerm = outputTerm.transform(t);
	    }
	    
	    return outputTerm;
	}

	/**
	 * Convertuje postac tekstowa relacji na typ
	 * @param type
	 * @return
	 */
	private static RelationType string2type(String type) {
		
		if(type.compareTo("=") == 0)
			return RelationType.EQUALITY;
		else if(type.compareTo("!=") == 0)
			return RelationType.INEQUALITY;
		else if(type.compareTo(">") == 0)
			return RelationType.GREATER_THEN;
		else if(type.compareTo(">=") == 0)
			return RelationType.GREATER_THEN_OR_EQUAL_TO;
		else if(type.compareTo("<") == 0)
			return RelationType.LESS_THEN;
		else if(type.compareTo("<=") == 0)
			return RelationType.LESS_THEN_OR_EQUAL_TO;
		
		return null;
	}

	/**
	 * Convertuje postac tekstowa relacji na typ
	 * @param type
	 * @return
	 */
	private static String type2string(RelationType type) {

		if(type == RelationType.EQUALITY)
			return "=";
		else if(type == RelationType.INEQUALITY)
			return "!=";
		else if(type == RelationType.GREATER_THEN)
			return ">";
		else if(type == RelationType.GREATER_THEN_OR_EQUAL_TO)
			return ">=";
		else if(type == RelationType.LESS_THEN)
			return "<";
		else if(type == RelationType.LESS_THEN_OR_EQUAL_TO)
			return "<=";
		
		return null;
	}

}
